﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License, as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;

namespace ExtremeML.Packaging
{
	/// <summary>
	/// Manages the <see cref="HyperlinkRelationship"/> instances for a single part container.
	/// </summary>
	/// <remarks>
	/// Unlike other classes in this namespace, this is not a wrapper. Instead it
	/// directly manages a collection of <see cref="HyperlinkRelationship"/> instances,
	/// representing the hyperlink relationships for an <see cref="OpenXmlPartContainer"/>.
	/// The reason a wrapper is not appropriate in this situation is that, unlike other
	/// wrapped classes <see cref="HyperlinkRelationship"/> does not derive from
	/// <see cref="OpenXmlPart"/> or <see cref="OpenXmlElement"/>.
	/// </remarks>
	internal sealed class HyperlinkRelationshipManager
	{
		#region Housekeeping

		/// <summary>
		/// Initializes a new instance of the <see cref="HyperlinkRelationshipManager"/> class.
		/// </summary>
		/// <param name="wrapped">An <see cref="OpenXmlPartContainer"/> from which to populate this instance.</param>
		public HyperlinkRelationshipManager(
			OpenXmlPartContainer wrapped)
		{
			Wrapped = wrapped;
		}

		#endregion
		#region Custom Properties

		/// <summary>
		/// Gets the <see cref="OpenXmlPartContainer"/> from which the contained collection is populated;
		/// </summary>
		internal OpenXmlPartContainer Wrapped { get; private set; }

		#endregion
		#region Custom Methods

		/// <summary>
		/// Returns the hyperlink relationship having the specified id.
		/// </summary>
		/// <param name="id">The unique id of the target cell hyperlink relationship.</param>
		/// <returns>A <see cref="HyperlinkRelationship"/> that matches the specified id. <c>null</c> if no match is found.</returns>
		internal HyperlinkRelationship Find(
			string id)
		{
			var result = Wrapped.GetReferenceRelationship(id);
			return result != null && result is HyperlinkRelationship ? result as HyperlinkRelationship : null;
		}

		/// <summary>
		/// Creates and adds a new <see cref="HyperlinkRelationship"/> using the passed <see cref="Uri"/>.
		/// </summary>
		/// <param name="uri">A <see cref="Uri"/> that represents the uri targeted by the relationship.</param>
		/// <returns>A <see cref="HyperlinkRelationship"/> that matches the specified value.</returns>
		internal HyperlinkRelationship Add(
			Uri uri)
		{
			return Wrapped.AddHyperlinkRelationship(uri, true);
		}

		/// <summary>
		/// Removes the passed <see cref="HyperlinkRelationship"/> from its containing collection.
		/// </summary>
		/// <param name="relationship"></param>
		internal void Remove(
			HyperlinkRelationship relationship)
		{
			Wrapped.DeleteReferenceRelationship(relationship);
		}

		#endregion
		#region Overrides


		#endregion
		#region Implementation Methods


		#endregion
		#region Static Methods


		#endregion
	}
}
